home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Recortar, arrastrar y colocar / ImageDrop / ImageDrop.cs next >
Encoding:
Text File  |  2002-05-30  |  2.7 KB  |  86 lines

  1. //----------------------------------------
  2. // ImageDrop.cs ⌐ 2001 by Charles Petzold
  3. //----------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Windows.Forms;
  9.  
  10. class ImageDrop: ImageClip
  11. {
  12.      bool bIsTarget;
  13.      [STAThread] 
  14.  
  15.      public new static void Main()
  16.      {
  17.          Application.Run(new ImageDrop());
  18.      }
  19.      public ImageDrop()
  20.      {
  21.           Text = strProgName = "Arrastrar y colocar imagen";
  22.  
  23.           AllowDrop = true;
  24.      }
  25.      protected override void OnDragOver(DragEventArgs dea)
  26.      {
  27.           if (dea.Data.GetDataPresent(DataFormats.FileDrop) ||
  28.               dea.Data.GetDataPresent(typeof(Metafile))     ||
  29.               dea.Data.GetDataPresent(typeof(Bitmap)))
  30.           {
  31.                if ((dea.AllowedEffect & DragDropEffects.Move) != 0)
  32.                     dea.Effect = DragDropEffects.Move;
  33.  
  34.                if (((dea.AllowedEffect & DragDropEffects.Copy) != 0) &&
  35.                    ((dea.KeyState & 0x08) != 0))    // Tecla Control
  36.                     dea.Effect = DragDropEffects.Copy;
  37.           }
  38.      }
  39.      protected override void OnDragDrop(DragEventArgs dea)
  40.      {
  41.           if (dea.Data.GetDataPresent(DataFormats.FileDrop))
  42.           {
  43.                string[] astr = (string[]) 
  44.                                    dea.Data.GetData(DataFormats.FileDrop);
  45.                try
  46.                {
  47.                     image = Image.FromFile(astr[0]);
  48.                }
  49.                catch (Exception exc)
  50.                {
  51.                     MessageBox.Show(exc.Message, Text);
  52.                     return;
  53.                }
  54.                strFileName = astr[0];
  55.                Text = strProgName + " - " + Path.GetFileName(strFileName);
  56.                Invalidate();
  57.           }
  58.           else 
  59.           {
  60.                if (dea.Data.GetDataPresent(typeof(Metafile)))
  61.                     image = (Image) dea.Data.GetData(typeof(Metafile));
  62.  
  63.                else if (dea.Data.GetDataPresent(typeof(Bitmap)))
  64.                     image = (Image) dea.Data.GetData(typeof(Bitmap));
  65.  
  66.                bIsTarget = true;
  67.                strFileName = "Arrastrar y colocar";
  68.                Text = strProgName + " - " + strFileName;
  69.                Invalidate();
  70.           }
  71.      }
  72.      protected override void OnMouseDown(MouseEventArgs mea)
  73.      {
  74.           if (image != null)
  75.           {
  76.                bIsTarget = false;
  77.  
  78.                DragDropEffects dde = DoDragDrop(image, 
  79.                               DragDropEffects.Copy | DragDropEffects.Move);
  80.  
  81.                if (dde == DragDropEffects.Move && !bIsTarget)
  82.                     image = null;
  83.           }
  84.      }
  85. }
  86.